﻿Imports System.Data

Imports System
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO

Partial Class Content_Resultados_creaPDF
    Inherits System.Web.UI.Page
    Dim IDataBase As New Clases.DataBase
    Dim ECConnString As String = ConfigurationManager.AppSettings("dbIP")
    Dim IControles As New Clases.controles
   


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim nombreReporte As String = "Movimientos.pdf"
        Dim dt As New DataTable

        IDataBase.createDataTable(dt, Session("RepMovi"))

        GenerarPdf(dt, nombreReporte)
        Response.Redirect("../../Files/" + nombreReporte)

    End Sub

    Protected Sub GenerarPdf(ByVal tblReporte As DataTable, ByVal NombreReporte As String)

        'Dimensiones del Docuemnto
        Dim documento As Document = New Document(PageSize.LETTER, 50, 100, 20, 100)
        ' step 2: creamos el documento            
        'Ruta donde se encuentra nuestra aplicacion, en esta ruta genera el Pdf esta ruta es la del servidor
        Dim writerPdf As PdfWriter = PdfWriter.GetInstance(documento, New FileStream(AppDomain.CurrentDomain.BaseDirectory & "Files\" & NombreReporte, FileMode.Create))

        'step 3: abrimos el documento
        documento.Open()

        'Titulo del archivo
        Dim font24B As Font = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 14, 0, BaseColor.WHITE)
        '

        Dim cb As PdfContentByte = writerPdf.DirectContent
        Dim ct As ColumnText = New ColumnText(cb)



        Dim logo As Image = iTextSharp.text.Image.GetInstance(Server.MapPath("../../Images/logo.jpg"))
        logo.SetAbsolutePosition(200, 750)
        logo.ScalePercent(75)
        documento.Add(logo)

        Dim seperator As iTextSharp.text.pdf.draw.VerticalPositionMark = New iTextSharp.text.pdf.draw.LineSeparator
        seperator.Offset = -30

        documento.Add(seperator)

        ct.Go()
        'Creacion de la tabla que se mostrara en el Pdf
        Dim table As PdfPTable = New PdfPTable(7)

        'actual width of table in points
        table.TotalWidth = 500

        'fix the absolute width of the table
        table.LockedWidth = True
        'documento.Add(table)
        table.SplitLate = True
        table.SplitRows = True


        Dim cell As PdfPCell = New PdfPCell(New Phrase("Reporte de Movimientos ", font24B))
        cell.Colspan = 7
        cell.HorizontalAlignment = 1
        cell.BackgroundColor = New BaseColor(53, 144, 165)
        table.AddCell(cell)

        Dim LetraTituloTabla As Font = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10, Font.BOLDITALIC)

        'Cremos las cabeceras de la tabla
        Dim celdaopera As PdfPCell = New PdfPCell(New Phrase("Operación", LetraTituloTabla))
        Dim celdaNombre As PdfPCell = New PdfPCell(New Phrase("Nombre", LetraTituloTabla))
        Dim celdaContrat As PdfPCell = New PdfPCell(New Phrase("Contrato", LetraTituloTabla))
        Dim celdaDesp As PdfPCell = New PdfPCell(New Phrase("Despacho", LetraTituloTabla))
        Dim celdaInst As PdfPCell = New PdfPCell(New Phrase("Institución", LetraTituloTabla))
        Dim celdaF As PdfPCell = New PdfPCell(New Phrase("Fecha", LetraTituloTabla))
        Dim celdaMonto As PdfPCell = New PdfPCell(New Phrase("Monto", LetraTituloTabla))




        'Adicionamos las cabeceras a la tabla
        table.AddCell(celdaopera)
        table.AddCell(celdaNombre)
        table.AddCell(celdaContrat)
        table.AddCell(celdaDesp)
        table.AddCell(celdaInst)
        table.AddCell(celdaF)
        table.AddCell(celdaMonto)




        ' recorremos todos las filas del Datatable
        For Each fila As DataRow In tblReporte.Rows

            table.AddCell(fila(0).ToString())
            table.AddCell(fila(1).ToString())
            table.AddCell(fila(2).ToString())
            table.AddCell(fila(3).ToString())
            table.AddCell(fila(4).ToString())
            table.AddCell(fila(5).ToString())
            table.AddCell(fila(6).ToString())

        Next


        table.WriteSelectedRows(0, -1, 50, 720, writerPdf.DirectContent)

        documento.Close()

    End Sub
End Class
